import * as _ from "lodash"
import { camelCase } from "../case"
import httpClient from "../httpclient"

export class DailySale {
  public date: string
  public quantity: number
  public amount: number
}

export class MonthlySale {
  public yearMonth: string
  public month: string
  public quantity: number
  public amount: number
}

export class SalesQuery {

  private params = {}

  public storeId(storeId: string): SalesQuery {
    this.params["store_id"] = storeId
    return this
  }

  public date(date: string | number): SalesQuery {
    this.params["date"] = date
    return this
  }

  public dateFrom(dateFrom: string | number): SalesQuery {
    this.params["date_from"] = dateFrom
    return this
  }

  public dateTo(dateTo: string | number): SalesQuery {
    this.params["date_to"] = dateTo
    return this
  }
}

export class TotalSales {
  public quantity: number
  public amount: number
}

export class SalesReportV1 {

  public static createSalesQuery() {
    return new SalesQuery()
  }

  public static queryDailySales(query: SalesQuery): Promise<DailySale[]> {
    return httpClient.get("/v1/reports/daily-sales", {
      params: query["params"],
    })
      .then(({ data }) => data)
      .then(camelCase)
  }

  public static queryMonthlySales(query: SalesQuery): Promise<MonthlySale[]> {
    return httpClient.get("/v1/reports/monthly-sales", {
      params: query["params"],
    })
      .then(({ data }) => data)
      .then(camelCase)
  }

  public static queryTotalSales(query: SalesQuery): Promise<TotalSales> {
    return httpClient.get("/v1/reports/total-sales", {
      params: query["params"],
    })
      .then(({ data }) => data)
      .then(data => _.assign(new TotalSales(), data))
  }
}
